﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class wishList2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnSubmit.Click += new ImageClickEventHandler(btnSubmit_Click);
        
    }

    void btnSubmit_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            //Chimes.DataContext context = new Chimes.DataContext();
            //Chimes.Competition comp = new Chimes.Competition();

            Chimes.DataContext context = new Chimes.DataContext();
            Chimes.Wish wishitem = new Chimes.Wish();

            wishitem.FirstName = txtFirstname.Text;
            wishitem.Surname = txtSurname.Text;
            wishitem.Email = txtEmail.Text;
            wishitem.Sex = Request.Form["gir_sex"];
            wishitem.Addres1 = txtAddres1.Text;
            wishitem.Address2 = txtAddress2.Text;
            wishitem.PostCode = txtPostCode.Text;
            wishitem.Friends = txtFriends.Text;
            wishitem.ShirtSize = txtShirtSize.Text;
            wishitem.CollarSize = txtCollarSize.Text;
            wishitem.WaistSize = txtWaistSize.Text;
            wishitem.InsideLeg = txtInsideLeg.Text;
            wishitem.DressSize = txtDressSize.Text;
            wishitem.TopSize = txtTopSize.Text;
            wishitem.SkirtSize = txtSkirtSize.Text;
            wishitem.ShoeSize = txtShoeSize.Text;
            wishitem.BraSize = txtBraSize.Text;
            wishitem.RingSize = txtRingSize.Text;
            wishitem.FavouriteColours = txtFavouriteColours.Text;
            wishitem.FavouriteSports = txtFavouriteSports.Text;
            wishitem.FavouriteAftershaves = txtFavouriteAftershaves.Text;
            wishitem.FavouritePerfumes = txtFavouritePerfumes.Text;
            wishitem.FavouriteCosmeticBrands = txtFavouriteCosmeticBrands.Text;
            wishitem.FavouriteClothingBrands = txtFavouriteClothingBrands.Text;
            wishitem.FavouriteShops = txtFavouriteShops.Text;
            wishitem.MusicalPreference = txtMusicalPreference.Text;
            wishitem.Hobbies = txtHobbies.Text;
            wishitem.WhatINeed = txtWhatINeed.Text;
            wishitem.WhatIDontNeed = txtWhatIDontNeed.Text;
            wishitem.OtherInfo = txtOtherInfo.Text;
            wishitem.OptOut = chkOptOut.Checked;

            context.Wishes.InsertOnSubmit(wishitem);
            context.SubmitChanges();


            Email emailSender = new Email(wishitem.Email, Server.MapPath("~/email/WishListToSender.xml"));
            emailSender.Format("Name", wishitem.FirstName);
            emailSender.Send();


            Email emailFriends = new Email(null, Server.MapPath("~/email/WishListToFriends.xml"));
            emailFriends.AddBccAddress(wishitem.Friends.Replace(",", ";"));

            string displayName = string.Concat(wishitem.FirstName.Trim(), " ", wishitem.Surname.Trim()).Trim();
            emailFriends.Format("FriendName", displayName);
            emailFriends.SetFrom(wishitem.Email);

            System.Text.StringBuilder stringList = new System.Text.StringBuilder();

            if (!string.IsNullOrEmpty(wishitem.ShirtSize))
            {
                stringList.AppendFormat("Shirt Size : {0}\n", wishitem.ShirtSize);
            }
            if (!string.IsNullOrEmpty(wishitem.CollarSize))
            {
                stringList.AppendFormat("Collar Size : {0}\n", wishitem.CollarSize);
            }
            if (!string.IsNullOrEmpty(wishitem.WaistSize))
            {
                stringList.AppendFormat("Waist Size : {0}\n", wishitem.WaistSize);
            }
            if (!string.IsNullOrEmpty(wishitem.InsideLeg))
            {
                stringList.AppendFormat("Inside Leg : {0}\n", wishitem.InsideLeg);
            }
            if (!string.IsNullOrEmpty(wishitem.DressSize))
            {
                stringList.AppendFormat("Dress Size : {0}\n", wishitem.DressSize);
            }
            if (!string.IsNullOrEmpty(wishitem.TopSize))
            {
                stringList.AppendFormat("Top Size : {0}\n", wishitem.TopSize);
            }
            if (!string.IsNullOrEmpty(wishitem.SkirtSize))
            {
                stringList.AppendFormat("Skirt Size : {0}\n", wishitem.SkirtSize);
            }
            if (!string.IsNullOrEmpty(wishitem.ShoeSize))
            {
                stringList.AppendFormat("Shoe Size : {0}\n", wishitem.ShoeSize);
            }
            if (!string.IsNullOrEmpty(wishitem.BraSize))
            {
                stringList.AppendFormat("Bra Size : {0}\n", wishitem.BraSize);
            }
            if (!string.IsNullOrEmpty(wishitem.RingSize))
            {
                stringList.AppendFormat("Ring Size : {0}\n", wishitem.RingSize);
            }
            if (!string.IsNullOrEmpty(wishitem.FavouriteColours))
            {
                stringList.AppendFormat("Favourite Colours : {0}\n", wishitem.FavouriteColours);
            }
            if (!string.IsNullOrEmpty(wishitem.FavouriteSports))
            {
                stringList.AppendFormat("Favourite Sports : {0}\n", wishitem.FavouriteSports);
            }
            if (!string.IsNullOrEmpty(wishitem.FavouriteAftershaves))
            {
                stringList.AppendFormat("Favourite Aftershaves : {0}\n", wishitem.FavouriteAftershaves);
            }
            if (!string.IsNullOrEmpty(wishitem.FavouritePerfumes))
            {
                stringList.AppendFormat("Favourite Perfumes : {0}\n", wishitem.FavouritePerfumes);
            }
            if (!string.IsNullOrEmpty(wishitem.FavouriteCosmeticBrands))
            {
                stringList.AppendFormat("Favourite Cosmetic Brands : {0}\n", wishitem.FavouriteCosmeticBrands);
            }
            if (!string.IsNullOrEmpty(wishitem.FavouriteClothingBrands))
            {
                stringList.AppendFormat("Favourite Clothing Brands : {0}\n", wishitem.FavouriteClothingBrands);
            }
            if (!string.IsNullOrEmpty(wishitem.FavouriteShops))
            {
                stringList.AppendFormat("Favourite Shops : {0}\n", wishitem.FavouriteShops);
            }
            if (!string.IsNullOrEmpty(wishitem.MusicalPreference))
            {
                stringList.AppendFormat("Musical Preference : {0}\n", wishitem.MusicalPreference);
            }
            if (!string.IsNullOrEmpty(wishitem.Hobbies))
            {
                stringList.AppendFormat("Hobbies : {0}\n", wishitem.Hobbies);
            }
            if (!string.IsNullOrEmpty(wishitem.WhatINeed))
            {
                stringList.AppendFormat("\nWhat I Need :\n{0}\n", wishitem.WhatINeed);
            }
            if (!string.IsNullOrEmpty(wishitem.WhatIDontNeed))
            {
                stringList.AppendFormat("\nWhat I Don't Need:\n{0}\n", wishitem.WhatIDontNeed);
            }
            if (!string.IsNullOrEmpty(wishitem.OtherInfo))
            {
                stringList.AppendFormat("\nOther Info :\n {0}\n", wishitem.OtherInfo);
            }
            emailFriends.Format("WishList", stringList.ToString());
            emailFriends.Send();

            pnlThankyou.Visible = true;
            pnlEntry.Visible = false;
        }

    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = chkTC.Checked;
    }
        
    protected void btnOpenPnlB_Click(object sender, EventArgs e)
    {
        pnlEntryA.Visible = false;
        pnlEntryB.Visible = true;

        if (Request.Form["gir_sex"] == "Male")
        {
            pnlShirtSize.Visible = true;
            pnlCollarSize.Visible = true;
            pnlWaistSize.Visible = true;
            pnlInsideLeg.Visible = true;
            pnlFavouriteSports.Visible = true;
            pnlFavouriteAftershaves.Visible = true;

            pnlDressSize.Visible = false;
            pnlTopSize.Visible = false;
            pnlSkirtSize.Visible = false;
            pnlBraSize.Visible = false;
            pnlFavouritePerfumes.Visible = false;
        }
        else {
            pnlShirtSize.Visible = false;
            pnlCollarSize.Visible = false;
            pnlWaistSize.Visible = false;
            pnlInsideLeg.Visible = false;
            pnlFavouriteSports.Visible = false;
            pnlFavouriteAftershaves.Visible = false;

            pnlDressSize.Visible = true;
            pnlTopSize.Visible = true;
            pnlSkirtSize.Visible = true;
            pnlBraSize.Visible = true;
            pnlFavouritePerfumes.Visible = true;
        }
    }
}
